package com.ruoyi.web.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.config.pay.CommonFieldName;
import com.ruoyi.common.config.pay.OrderIdGenerator;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.web.request.IESI123107Request;
import com.ruoyi.web.request.IESI123109Request;
import com.ruoyi.web.service.AgentPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.PayRecordDownloadMapper;
import com.ruoyi.web.domain.PayRecordDownload;
import com.ruoyi.web.service.IPayRecordDownloadService;
import com.ruoyi.common.core.text.Convert;

/**
 * 商户代付对账文件Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-04
 */
@Service
public class PayRecordDownloadServiceImpl implements IPayRecordDownloadService 
{
    @Autowired
    private PayRecordDownloadMapper payRecordDownloadMapper;
    @Autowired
    private AgentPayService agentPayService;
    /**
     * 查询商户代付对账文件
     * 
     * @param sequenceNo 商户代付对账文件ID
     * @return 商户代付对账文件
     */
    @Override
    public PayRecordDownload selectPayRecordDownloadById(String sequenceNo)
    {
        return payRecordDownloadMapper.selectPayRecordDownloadById(sequenceNo);
    }

    /**
     * 查询商户代付对账文件列表
     * 
     * @param payRecordDownload 商户代付对账文件
     * @return 商户代付对账文件
     */
    @Override
    public List<PayRecordDownload> selectPayRecordDownloadList(PayRecordDownload payRecordDownload)
    {
        return payRecordDownloadMapper.selectPayRecordDownloadList(payRecordDownload);
    }

    /**
     * 新增商户代付对账文件
     * 
     * @param payRecordDownload 商户代付对账文件
     * @return 结果
     */
    @Override
    public int insertPayRecordDownload(PayRecordDownload payRecordDownload) throws Exception
    {
        BusinessException.notNull(payRecordDownload.getCheckDate(),"请填写对账日期");
        IESI123107Request request =new IESI123107Request();
        request.setAddInfo(payRecordDownload.getAddInfo());
        request.setCheckDate(payRecordDownload.getCheckDate());
        request.setCheckType("DFFILE");
        payRecordDownload.setSequenceNo(OrderIdGenerator.getDefault().nextId());
        HashMap<String, Object> resultMap= agentPayService.IESI123107(request,payRecordDownload.getSequenceNo(),payRecordDownload.getMerchCd());
        String respCode = (String) resultMap.get(CommonFieldName.RESP_CODE);
        String respMsg = (String) resultMap.get(CommonFieldName.RESP_MSG);
        if (!"00000".equals(respCode)){
            BusinessException.notNull(null,respMsg);
        }
        payRecordDownload.setDownloadUrl((String) resultMap.get("checkUrl"));
        return payRecordDownloadMapper.insertPayRecordDownload(payRecordDownload);
    }

    /**
     * 修改商户代付对账文件
     * 
     * @param payRecordDownload 商户代付对账文件
     * @return 结果
     */
    @Override
    public int updatePayRecordDownload(PayRecordDownload payRecordDownload)
    {
        return payRecordDownloadMapper.updatePayRecordDownload(payRecordDownload);
    }

    /**
     * 删除商户代付对账文件对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePayRecordDownloadByIds(String ids)
    {
        return payRecordDownloadMapper.deletePayRecordDownloadByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商户代付对账文件信息
     * 
     * @param sequenceNo 商户代付对账文件ID
     * @return 结果
     */
    @Override
    public int deletePayRecordDownloadById(String sequenceNo)
    {
        return payRecordDownloadMapper.deletePayRecordDownloadById(sequenceNo);
    }
}
